/*
 Copyright 2012 Sven-Ove Bjerkan

 This file is part of KO-Ozi.

 KO-Ozi is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 KO-Ozi is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with KO-Ozi. If not, see <http://www.gnu.org/licenses/>.
 */
package com.koozi.ui;

import com.koozi.Screenshot;
import com.koozi.Settings;
import com.koozi.TimeAndDate;
import com.koozi.WildCardFileFilter;
import com.koozi.auto.OziAutoload;
import com.koozi.auto.OziAutosave;
import com.koozi.backup.Backup;
import com.koozi.ui.tracking.TrackerTableModel;
import com.koozi.ui.tracking.TrackingSettings;
import com.oziexplorer.LatLon;
import com.oziexplorer.ObjectClickCallback;
import com.oziexplorer.ObjectClickType;
import com.oziexplorer.UTMGridRef;
import java.awt.Frame;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ComboBoxModel;
import javax.swing.JOptionPane;

/**
 *
 * @author Sven-Ove Bjerkan
 */
public class MainTabWindow extends javax.swing.JFrame implements ObjectClickCallback {

    private final Settings settings;
    private TrackerTableModel trackerTabMod;
    private ComboBoxModel trackingCombo;
    private Thread th;
    private OziAutosave oas;
    private OziAutoload oal;
    private Thread backupThread;

    /**
     * Creates new form MainTabWindow
     */
    public MainTabWindow() {
        this.setTitle("KO-Ozi - v0.4 alpha");
        
        settings = Settings.getInstance();

        trackerTabMod = new TrackerTableModel();
        try {
            trackingCombo = new javax.swing.DefaultComboBoxModel(new String[]{
                        "- VELG -", "Denne maskinen (" + InetAddress.getLocalHost().getHostName() + ")"});
        } catch (UnknownHostException ex) {
            ex.printStackTrace();
        }

        initComponents();

        btnStop.setVisible(false);
        lastUpdatePane.setVisible(false);

        try {
            paneAbout.setPage(Settings.class.getResource("about.htm"));
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        // If just 1 monitor, hide the monitor-selector
        if (Screenshot.getNumScreens() == 1) {
            txtMonitorNo.setVisible(false);
            spnMonitorNo.setVisible(false);
        } else {
            spnMonitorNo.setModel(new javax.swing.SpinnerNumberModel(1, 1,
                    Screenshot.getNumScreens(), 1));
        }

        // Set the projectname if a project exists
        File dir = new File(settings.getPath() + "/");
        File[] files = dir.listFiles(new WildCardFileFilter(settings.getDefaultProjName() + "*"));

        for (File file : files) {
            if (file.isDirectory()) {
                fldProjName.setText(file.getName());
                break;
            }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabbedPane = new javax.swing.JTabbedPane();
        panelMain = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        fldProjName = new javax.swing.JTextField();
        btnChooseProjName = new javax.swing.JButton();
        btnStop = new javax.swing.JButton();
        btnMaster = new javax.swing.JToggleButton();
        btnSlave = new javax.swing.JToggleButton();
        lastUpdatePane = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        lblLastupdate = new javax.swing.JLabel();
        btnManUpdate = new javax.swing.JButton();
        panelScreenshot = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        lstPhoto = new javax.swing.JList();
        btnShowScreenshot = new javax.swing.JButton();
        btnDeleteScreenshot = new javax.swing.JButton();
        txtMonitorNo = new javax.swing.JLabel();
        spnMonitorNo = new javax.swing.JSpinner();
        btnScreenshot = new javax.swing.JButton();
        panelTracking = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        trackerTable = new javax.swing.JTable();
        btnTrackingSettings = new javax.swing.JButton();
        jLabel11 = new javax.swing.JLabel();
        selTrackerAdmin = new javax.swing.JComboBox();
        btnLagreTracking = new javax.swing.JButton();
        paneSettings = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        fldPath = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        btnLagre = new javax.swing.JButton();
        spnBackupInterval = new javax.swing.JSpinner();
        spnNumTracksSave = new javax.swing.JSpinner();
        btnChooseFolder = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        spnUpdInterval = new javax.swing.JSpinner();
        jLabel4 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        paneAbout = new javax.swing.JTextPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Prosjektnavn:");

        fldProjName.setText(Settings.getInstance().getDefaultProjName());

        btnChooseProjName.setText("Velg");
        btnChooseProjName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChooseProjNameActionPerformed(evt);
            }
        });

        btnStop.setText("Stopp");
        btnStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStopActionPerformed(evt);
            }
        });

        btnMaster.setText("Dette er maskinen til NK/leder");
        btnMaster.setToolTipText("Dvs at det er denne maskinen man setter inn veipunkt, administrerer oppdrag, o.l");
        btnMaster.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMasterActionPerformed(evt);
            }
        });

        btnSlave.setText("Dette er maskinen til samb.leder eller andre");
        btnSlave.setToolTipText("Dvs at denne maskinen bare skal \"se\" på kartet, veipunkt, oppdrag, o.l");
        btnSlave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSlaveActionPerformed(evt);
            }
        });

        jLabel5.setText("Siste oppdatering:");

        lblLastupdate.setText("Aldri");

        btnManUpdate.setText("Manuel oppdatering");
        btnManUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnManUpdateActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout lastUpdatePaneLayout = new javax.swing.GroupLayout(lastUpdatePane);
        lastUpdatePane.setLayout(lastUpdatePaneLayout);
        lastUpdatePaneLayout.setHorizontalGroup(
            lastUpdatePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(lastUpdatePaneLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(lastUpdatePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(lastUpdatePaneLayout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(18, 18, 18)
                        .addComponent(lblLastupdate))
                    .addComponent(btnManUpdate))
                .addContainerGap(46, Short.MAX_VALUE))
        );
        lastUpdatePaneLayout.setVerticalGroup(
            lastUpdatePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(lastUpdatePaneLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(lastUpdatePaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblLastupdate)
                    .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnManUpdate)
                .addGap(15, 15, 15))
        );

        javax.swing.GroupLayout panelMainLayout = new javax.swing.GroupLayout(panelMain);
        panelMain.setLayout(panelMainLayout);
        panelMainLayout.setHorizontalGroup(
            panelMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelMainLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelMainLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(fldProjName, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnChooseProjName))
                    .addGroup(panelMainLayout.createSequentialGroup()
                        .addGroup(panelMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(btnSlave, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnMaster, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnStop))
                    .addComponent(lastUpdatePane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(249, Short.MAX_VALUE))
        );
        panelMainLayout.setVerticalGroup(
            panelMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelMainLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panelMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(fldProjName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnChooseProjName))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnStop)
                    .addComponent(btnMaster))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSlave)
                .addGap(18, 18, 18)
                .addComponent(lastUpdatePane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(42, Short.MAX_VALUE))
        );

        tabbedPane.addTab("Hovedvindu", panelMain);

        lstPhoto.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        lstPhoto.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lstPhotoMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(lstPhoto);

        btnShowScreenshot.setText("Vis");
        btnShowScreenshot.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnShowScreenshotActionPerformed(evt);
            }
        });

        btnDeleteScreenshot.setText("Slett");
        btnDeleteScreenshot.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteScreenshotActionPerformed(evt);
            }
        });

        txtMonitorNo.setText("Ta bilde av skjerm nummer");

        spnMonitorNo.setModel(new javax.swing.SpinnerNumberModel(1, 1, 2, 1));

        btnScreenshot.setText("Ta skjermdump");
        btnScreenshot.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnScreenshotActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelScreenshotLayout = new javax.swing.GroupLayout(panelScreenshot);
        panelScreenshot.setLayout(panelScreenshotLayout);
        panelScreenshotLayout.setHorizontalGroup(
            panelScreenshotLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelScreenshotLayout.createSequentialGroup()
                .addContainerGap(284, Short.MAX_VALUE)
                .addComponent(txtMonitorNo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(spnMonitorNo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnScreenshot)
                .addContainerGap())
            .addGroup(panelScreenshotLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(panelScreenshotLayout.createSequentialGroup()
                    .addGap(19, 19, 19)
                    .addGroup(panelScreenshotLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 529, Short.MAX_VALUE)
                        .addGroup(panelScreenshotLayout.createSequentialGroup()
                            .addComponent(btnShowScreenshot)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(btnDeleteScreenshot)
                            .addGap(0, 423, Short.MAX_VALUE)))
                    .addGap(19, 19, 19)))
        );
        panelScreenshotLayout.setVerticalGroup(
            panelScreenshotLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelScreenshotLayout.createSequentialGroup()
                .addGap(0, 203, Short.MAX_VALUE)
                .addGroup(panelScreenshotLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnScreenshot)
                    .addComponent(spnMonitorNo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtMonitorNo)))
            .addGroup(panelScreenshotLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(panelScreenshotLayout.createSequentialGroup()
                    .addGap(12, 12, 12)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(panelScreenshotLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnShowScreenshot)
                        .addComponent(btnDeleteScreenshot))
                    .addContainerGap(43, Short.MAX_VALUE)))
        );

        tabbedPane.addTab("Skjermdump", panelScreenshot);

        trackerTable.setModel(trackerTabMod);
        trackerTable.setEnabled(false);
        trackerTable.getTableHeader().setReorderingAllowed(false);
        jScrollPane3.setViewportView(trackerTable);

        btnTrackingSettings.setText("Innstillinger");
        btnTrackingSettings.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTrackingSettingsActionPerformed(evt);
            }
        });

        jLabel11.setText("Trackere administreres av:");

        selTrackerAdmin.setModel(trackingCombo);

        btnLagreTracking.setText("Lagre");
        btnLagreTracking.setEnabled(false);
        btnLagreTracking.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLagreTrackingActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelTrackingLayout = new javax.swing.GroupLayout(panelTracking);
        panelTracking.setLayout(panelTrackingLayout);
        panelTrackingLayout.setHorizontalGroup(
            panelTrackingLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelTrackingLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnTrackingSettings)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 227, Short.MAX_VALUE)
                .addComponent(jLabel11)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(selTrackerAdmin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(14, 14, 14)
                .addComponent(btnLagreTracking)
                .addContainerGap())
            .addGroup(panelTrackingLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(panelTrackingLayout.createSequentialGroup()
                    .addGap(9, 9, 9)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 549, Short.MAX_VALUE)
                    .addGap(9, 9, 9)))
        );
        panelTrackingLayout.setVerticalGroup(
            panelTrackingLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelTrackingLayout.createSequentialGroup()
                .addGap(0, 203, Short.MAX_VALUE)
                .addGroup(panelTrackingLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnLagreTracking)
                    .addComponent(selTrackerAdmin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11)
                    .addComponent(btnTrackingSettings)))
            .addGroup(panelTrackingLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(panelTrackingLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(29, Short.MAX_VALUE)))
        );

        tabbedPane.addTab("Tracking", panelTracking);

        jLabel7.setText("Hovedmappe for lagring:");

        fldPath.setEditable(false);
        fldPath.setText(settings.getPath());

        jLabel6.setText("Antall spor som skal lagres/åpnes:");

        jLabel13.setText("Ta backup hvert:");

        btnLagre.setText("Lagre");
        btnLagre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLagreActionPerformed(evt);
            }
        });

        spnBackupInterval.setModel(new javax.swing.SpinnerNumberModel(10, 2, 60, 2));
        spnBackupInterval.setValue(settings.getBackupInterval());

        spnNumTracksSave.setModel(new javax.swing.SpinnerNumberModel(50, 1, 500, 5));
        spnNumTracksSave.setValue(settings.getSaveTracks());

        btnChooseFolder.setText("Velg");
        btnChooseFolder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChooseFolderActionPerformed(evt);
            }
        });

        jLabel3.setText("Lagring-/åpningsintervall:");

        spnUpdInterval.setModel(new javax.swing.SpinnerNumberModel(0.5d, 0.25d, 30.0d, 0.25d));
        spnUpdInterval.setValue(settings.getOpenSaveInterval());

        jLabel4.setText("min");

        jLabel8.setText("min");

        javax.swing.GroupLayout paneSettingsLayout = new javax.swing.GroupLayout(paneSettings);
        paneSettings.setLayout(paneSettingsLayout);
        paneSettingsLayout.setHorizontalGroup(
            paneSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(paneSettingsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(paneSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(paneSettingsLayout.createSequentialGroup()
                        .addComponent(fldPath)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnChooseFolder))
                    .addGroup(paneSettingsLayout.createSequentialGroup()
                        .addGroup(paneSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(btnLagre)
                            .addGroup(paneSettingsLayout.createSequentialGroup()
                                .addGroup(paneSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel6)
                                    .addComponent(jLabel13)
                                    .addComponent(jLabel3))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(paneSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(paneSettingsLayout.createSequentialGroup()
                                        .addComponent(spnUpdInterval, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jLabel4))
                                    .addGroup(paneSettingsLayout.createSequentialGroup()
                                        .addGroup(paneSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(spnBackupInterval)
                                            .addComponent(spnNumTracksSave, javax.swing.GroupLayout.DEFAULT_SIZE, 55, Short.MAX_VALUE))
                                        .addGap(10, 10, 10)
                                        .addComponent(jLabel8)))))
                        .addGap(0, 290, Short.MAX_VALUE)))
                .addGap(19, 19, 19))
        );
        paneSettingsLayout.setVerticalGroup(
            paneSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(paneSettingsLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(paneSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fldPath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnChooseFolder))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(paneSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(spnNumTracksSave, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(paneSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13)
                    .addComponent(spnBackupInterval, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(paneSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(spnUpdInterval, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 60, Short.MAX_VALUE)
                .addComponent(btnLagre)
                .addContainerGap())
        );

        tabbedPane.addTab("Innstillinger", paneSettings);

        paneAbout.setContentType(
            "text/html");
        paneAbout.setEditable(false);
        jScrollPane1.setViewportView(paneAbout);

        tabbedPane.addTab("Om", jScrollPane1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tabbedPane)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tabbedPane)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnManUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnManUpdateActionPerformed
        th.interrupt();
        if (oas != null) {
            th = new Thread(oas);
        } else if (oal != null) {
            th = new Thread(oal);
        }
        th.start();
    }//GEN-LAST:event_btnManUpdateActionPerformed

    private void lstPhotoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lstPhotoMouseClicked
        if (evt.getClickCount() == 2 && lstPhoto.getSelectedIndex() != -1) {
            this.setAlwaysOnTop(false);
            Screenshot.showScreenshot((String) lstPhoto.getSelectedValue());
        }
    }//GEN-LAST:event_lstPhotoMouseClicked

    private void btnShowScreenshotActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnShowScreenshotActionPerformed
        if (lstPhoto.getSelectedIndex() != -1) {
            this.setAlwaysOnTop(false);
            Screenshot.showScreenshot((String) lstPhoto.getSelectedValue());
        }
    }//GEN-LAST:event_btnShowScreenshotActionPerformed

    private void btnDeleteScreenshotActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteScreenshotActionPerformed
        if (lstPhoto.getSelectedIndex() != -1) {
            if (JOptionPane.showConfirmDialog(null,
                    "Er du sikker på at du vil slette bildet?", "Slette skjermdump",
                    JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                String selFile = settings.getProjectPath() + "/Skjermdumper/"
                        + (String) lstPhoto.getSelectedValue();
                File f = new File(selFile);
                f.delete();
                updateFileList();
            }
        }
    }//GEN-LAST:event_btnDeleteScreenshotActionPerformed

    private void btnScreenshotActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnScreenshotActionPerformed
        Screenshot ss = new Screenshot();

        this.setState(Frame.ICONIFIED);
        try {
            Thread.sleep(500);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }

        ss.takeScreenshot(Integer.parseInt(spnMonitorNo.getValue().toString()),
                "Kartbilde - " + fldProjName.getText() + " - kl "
                + TimeAndDate.getNiceTime(".") + ".png");
        this.setState(Frame.NORMAL);

        if (updateFileList() == 1 && Screenshot.getNumScreens() > 1) {
            JOptionPane.showMessageDialog(null, "Vennligst kontroller at du har tatt skjermbilde av riktig skjerm.");
        }
    }//GEN-LAST:event_btnScreenshotActionPerformed

    private void btnTrackingSettingsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTrackingSettingsActionPerformed
        TrackingSettings ts = new TrackingSettings();
        ts.setTitle("Tracking-innstillinger");
        ts.setVisible(true);
    }//GEN-LAST:event_btnTrackingSettingsActionPerformed

    private void btnLagreTrackingActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLagreTrackingActionPerformed
    }//GEN-LAST:event_btnLagreTrackingActionPerformed

    private void btnLagreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLagreActionPerformed
        settings.setPath(fldPath.getText());
        settings.setSaveTracks(spnNumTracksSave.getValue().toString());
        settings.setBackupInterval(spnBackupInterval.getValue().toString());
        settings.setOpenSaveInterval(spnUpdInterval.getValue().toString());
        if (settings.save()) {
            JOptionPane.showMessageDialog(null, "Innstillinger lagret");
        } else {
            JOptionPane.showMessageDialog(null, "En feil oppstod ved lagring");
        }
    }//GEN-LAST:event_btnLagreActionPerformed

    private void btnChooseFolderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChooseFolderActionPerformed
        MainFolderChooser fc = new MainFolderChooser(fldPath);
        fc.setVisible(true);
    }//GEN-LAST:event_btnChooseFolderActionPerformed

    private void btnChooseProjNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChooseProjNameActionPerformed
        ProjectFolderChooser pfc = new ProjectFolderChooser(fldProjName);
        pfc.setVisible(true);
    }//GEN-LAST:event_btnChooseProjNameActionPerformed

    private void btnMasterpressed(boolean masterPressed) {
        // Disable both buttons
        btnMaster.setEnabled(false);
        btnSlave.setEnabled(false);

        // Hide the one that isn't pressed
        btnMaster.setVisible(masterPressed);
        btnSlave.setVisible(!masterPressed);

        btnStop.setVisible(true);

        // Show last-update-pane
        lastUpdatePane.setVisible(true);

        // Disable project-, path-field and path-button
        fldProjName.setEnabled(false);
        fldPath.setEnabled(false);
        btnChooseFolder.setEnabled(false);
        btnChooseProjName.setEnabled(false);

        settings.setProjectPath(settings.getPath() + "/" + fldProjName.getText());
        updateFileList();

        btnLagreTracking.setEnabled(true);

        /*
         * updateTrackerCombo();
         *
         * if (trackerWatcher != null) { trackerWatcher.setOziAutosave(oas); }
         */
        if (masterPressed) {
            backupThread = new Thread(new Backup());
            backupThread.start();
        }
    }

    private void btnMasterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMasterActionPerformed
        fldProjName.setText(fldProjName.getText().trim());
        File f = new File(settings.getPath() + "/" + fldProjName.getText());
        if (f.isDirectory()) {
            int overwrite = JOptionPane.showConfirmDialog(null, "\"" + fldProjName.getText() + "\" finnes allerede, evt eksisterende data vil bli overskrevet.\nFortsett?", "Overskrive?", JOptionPane.YES_NO_OPTION);
            if (overwrite == JOptionPane.NO_OPTION) {
                return;
            }
        }

        btnMasterpressed(true);

        // Initiate a save-object
        oas = new OziAutosave(lblLastupdate);
        try {
            th = new Thread(oas);
            th.start();
        } catch (Exception ex) {
            Logger.getLogger(MainTabWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnMasterActionPerformed

    private void btnSlaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSlaveActionPerformed
        fldProjName.setText(fldProjName.getText().trim());
        btnMasterpressed(false);

        // Initiate load-object
        oal = new OziAutoload(lblLastupdate);
        try {
            th = new Thread(oal);
            th.start();
        } catch (Exception ex) {
            Logger.getLogger(MainTabWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
        JOptionPane.showMessageDialog(null, "Siden dette er en \"sekundærmaskin\", "
                + "kan du ikke gjøre egne endringer/legge til/slette veipunkter eller spor, "
                + "da de vil bli overskrevet av NK-/leder-maskinen.");
    }//GEN-LAST:event_btnSlaveActionPerformed

    private void btnStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStopActionPerformed
        // Enable both buttons
        btnMaster.setEnabled(true);
        btnSlave.setEnabled(true);

        btnMaster.setVisible(true);
        btnSlave.setVisible(true);

        btnMaster.setSelected(false);
        btnSlave.setSelected(false);

        btnStop.setVisible(false);

        // Hide last-update-pane
        lastUpdatePane.setVisible(false);

        // Enable project-, path-field and path-button
        fldProjName.setEnabled(true);
        fldPath.setEnabled(true);
        btnChooseFolder.setEnabled(true);
        btnChooseProjName.setEnabled(true);

        th.interrupt();
        oal = null;
        oas = null;

        if (backupThread != null && backupThread.isAlive()) {
            backupThread.interrupt();
        }
    }//GEN-LAST:event_btnStopActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainTabWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainTabWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainTabWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainTabWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MainTabWindow().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnChooseFolder;
    private javax.swing.JButton btnChooseProjName;
    private javax.swing.JButton btnDeleteScreenshot;
    private javax.swing.JButton btnLagre;
    private javax.swing.JButton btnLagreTracking;
    private javax.swing.JButton btnManUpdate;
    private javax.swing.JToggleButton btnMaster;
    private javax.swing.JButton btnScreenshot;
    private javax.swing.JButton btnShowScreenshot;
    private javax.swing.JToggleButton btnSlave;
    private javax.swing.JButton btnStop;
    private javax.swing.JButton btnTrackingSettings;
    private javax.swing.JTextField fldPath;
    private javax.swing.JTextField fldProjName;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JPanel lastUpdatePane;
    private javax.swing.JLabel lblLastupdate;
    private javax.swing.JList lstPhoto;
    private javax.swing.JTextPane paneAbout;
    private javax.swing.JPanel paneSettings;
    private javax.swing.JPanel panelMain;
    private javax.swing.JPanel panelScreenshot;
    private javax.swing.JPanel panelTracking;
    private javax.swing.JComboBox selTrackerAdmin;
    private javax.swing.JSpinner spnBackupInterval;
    private javax.swing.JSpinner spnMonitorNo;
    private javax.swing.JSpinner spnNumTracksSave;
    private javax.swing.JSpinner spnUpdInterval;
    private javax.swing.JTabbedPane tabbedPane;
    private javax.swing.JTable trackerTable;
    private javax.swing.JLabel txtMonitorNo;
    // End of variables declaration//GEN-END:variables

    @Override
    public void objectClickCallback(ObjectClickType oct, LatLon latlon, UTMGridRef utmgr, String string) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    private int updateFileList() {
        File folder = new File(settings.getProjectPath() + "/Skjermdumper/");
        File[] listofFiles = folder.listFiles();

        if (listofFiles == null) {
            String[] strings = {"Ingen bilder funnet"};
            lstPhoto.setListData(strings);
            lstPhoto.setEnabled(false);
            return 0;
        } else {
            String[] shortListOfFiles = new String[listofFiles.length];
            for (int i = 0; i < listofFiles.length; i++) {
                shortListOfFiles[i] = listofFiles[i].getName();
            }
            lstPhoto.setListData(shortListOfFiles);
            lstPhoto.setEnabled(true);
            return listofFiles.length;
        }
    }
}
